<template>
  <div class="register-container">
    <el-card class="register-card">
      <div class="register-title">注册</div>
      <el-form ref="registerForm" :model="registerForm" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="button-container">
            <el-button type="primary" @click="submitForm">注册</el-button>
            <el-button type="text" @click="goToLogin">登录</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from "@/Api/axiosInterceptor.ts";
import {headerConfig} from "@/Api/HeaderConfig.ts";

export default {
  data() {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '密码长度不能小于6位', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请再次输入密码', trigger: 'blur'},
          {validator: validateConfirmPassword, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    async submitForm() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          axios.post('/users/register', this.registerForm, headerConfig).then((response) => {
            this.$message.success('注册成功');
            this.$router.push('/login');
          }).catch((err)=> {
            this.$message.error('用户已经存在')
              this.$router.push('/login');
          })
        } else {
          console.log('表单验证失败!');
          return false;
        }
      });
    },
    goToLogin() {
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-image: url("../assets/img.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.register-card {
  width: 400px;
  padding: 20px;
  border-radius: 10px;
}

.register-title {
  font-size: 24px;
  margin-bottom: 20px;
  text-align: center;
}

.button-container {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>